package cn.anc.aonicardv.util;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.bean.RecorderConnectListBean;

public class AoniRecorderInfoUtils {

    public static void saveConnectedRecorderInfo(String wifiSSID, String wifiPassword, String brand) {
        String temp = SpUtils.getString(Constant.SpKeyParam.RECORDER_CONNECT_LIST, null);
        List<RecorderConnectListBean> recorderConnectListBeanList;
        if (temp == null) {
            recorderConnectListBeanList = new ArrayList<>();
            recorderConnectListBeanList.add(new RecorderConnectListBean(wifiSSID, brand, true, wifiPassword));
        } else {
            recorderConnectListBeanList = new Gson().fromJson(temp, new TypeToken<List<RecorderConnectListBean>>() {
            }.getType());
            if (!TextUtils.isEmpty(wifiSSID) && !temp.contains(wifiSSID)) {
                recorderConnectListBeanList.add(new RecorderConnectListBean(wifiSSID, brand, true, wifiPassword));
            } else {
                for (int i = 0; i < recorderConnectListBeanList.size(); i++) {
                    if (recorderConnectListBeanList.get(i).getWifiSSID().equals(wifiSSID)) {
                        recorderConnectListBeanList.get(i).setLast(true);
                        recorderConnectListBeanList.get(i).setWifiPassword(wifiPassword);
                    } else
                        recorderConnectListBeanList.get(i).setLast(false);
                }
            }
        }

        SpUtils.putString(Constant.SpKeyParam.RECORDER_CONNECT_LIST, new Gson().toJson(recorderConnectListBeanList));
    }


    public static void modifyConnectedRecorderInfo(String where, String key, String value) {
        String temp = SpUtils.getString(Constant.SpKeyParam.RECORDER_CONNECT_LIST, null);
        if (!TextUtils.isEmpty(temp)) {
            List<RecorderConnectListBean> recorderConnectBeanList = new Gson().fromJson(temp, new TypeToken<List<RecorderConnectListBean>>() {
            }.getType());
            JSONObject jsonObject = null;
            for (int i = 0; i < recorderConnectBeanList.size(); i++) {
                RecorderConnectListBean recorderConnectListBean = recorderConnectBeanList.get(i);
                if (recorderConnectListBean.getWifiSSID().equals(where)) {
                    String js = new Gson().toJson(recorderConnectListBean);
                    try {
                        jsonObject = new JSONObject(js);
                        jsonObject.put(key, value);
                        recorderConnectListBean = new Gson().fromJson(jsonObject.toString(), new TypeToken<RecorderConnectListBean>() {
                        }.getType());
                        recorderConnectBeanList.set(i, recorderConnectListBean);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
            if (jsonObject != null)
                SpUtils.putString(Constant.SpKeyParam.RECORDER_CONNECT_LIST, new Gson().toJson(recorderConnectBeanList));
        }
    }

}
